﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ImageComboBox;

namespace PayloadTracker
{
    public partial class EditFlight : Form
    {
        private Flight m_Flight;
        object[] m_ImageIndex = new object[] {"/", 0};
        APRSSymbols m_APRSSymbols = new APRSSymbols();

        public EditFlight(Flight flt)
        {
            InitializeComponent();
            m_Flight = flt;
        }

        void SetupColors()
        {
            List<string> colors = new List<string>();
            foreach (string s in Enum.GetNames(typeof(KnownColor)))
            {
                KnownColor k = (KnownColor)Enum.Parse(typeof(KnownColor), s);
                if (k >= KnownColor.Transparent)
                    colors.Add(s);
            }
            cbPlot.Items.AddRange(colors.ToArray());
            cbPlot.SelectedItem = m_Flight.LineColor.Name.ToString();
        }

        void SetupSymbols()
        {
            comboBoxEx1.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBoxEx1.ImageList = m_APRSSymbols.ImageList;
            for (int i = 0; i < m_APRSSymbols.SymbolMetaData.Length; i++)
            {
                comboBoxEx1.Items.Add(new ComboBoxExItem(m_APRSSymbols.SymbolMetaData[i][2], i));
            }
        }

        void SetupOverlays()
        {
            string[] strs = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
            System.Data.DataTable dt = new System.Data.DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("value");
            foreach (string str in strs)
            {
                string[] tuple = { str, str };
                dt.Rows.Add(tuple);
            }
            this.comboBox1.DataSource = dt;
            this.comboBox1.ValueMember = "value";
            this.comboBox1.DisplayMember = "name";
        }

        private void EditFlight_Load(object sender, EventArgs e)
        {
            m_ImageIndex = m_Flight.ImageIndex;
            SetupColors();
            SetupSymbols();
            SetupOverlays();
            comboBoxEx1.SelectedIndex = (int)m_ImageIndex[1];
            this.comboBox1.SelectedValue = m_ImageIndex[0].ToString();            
        }

        private void btnCancel_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click_1(object sender, EventArgs e)
        {
            m_Flight.ImageIndex = m_ImageIndex;
            m_Flight.LineColor = Color.FromName(cbPlot.SelectedItem as string);
            this.Close();
        }

        private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            m_ImageIndex[0] = comboBox1.SelectedValue;
        }

        private void comboBoxEx1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            int index = comboBoxEx1.SelectedIndex;
            int[] overlays = new int[] { 98, 101, 111, 125, 128, 150, 157, 158, 162, 173, 178, 181, 185 };
            m_ImageIndex[1] = index;
            if (index <= 95)
            {
                m_ImageIndex[0] = "/";
                comboBox1.Enabled = false;
            }
            else if (Array.IndexOf(overlays, comboBoxEx1.SelectedIndex) > -1)
            {
                m_ImageIndex[0] = "\\";
                comboBox1.Enabled = true;
                comboBox1.SelectedValue = m_ImageIndex[0].ToString();
            }
            else
            {
                m_ImageIndex[0] = "\\";
                comboBox1.Enabled = false;
            }
        }

        
    }
}
